package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.exception.ElearningMysqlException;
import com.wuwei.elearning.question.domain.Question;
import com.wuwei.elearning.question.domain.SubjectTest;
import com.wuwei.elearning.question.mapper.QuestionMapper;
import com.wuwei.elearning.question.mapper.SubjectTestMapper;
import com.wuwei.elearning.question.mapper.SubjectTestRelationQuestionMapper;
import com.wuwei.elearning.question.service.SubjectTestRelationQuestionService;
import com.wuwei.elearning.question.service.SubjectTestService;
import com.wuwei.elearning.utils.result.Result;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2025-03-12
 */
@RestController
@RequestMapping("/elearning/subjectTestRelationQuestion")
@CrossOrigin
public class SubjectTestRelationQuestionController {


    /**
     * 根据subjectTest的主键然后去subject_test_relation_question表查询question_id，然后批量获取question表的数据
     * @param subjectTestId subjectTest表的主键
     * @return
     */
    @GetMapping("batchGet/{subjectTestId}")
    public Result<HashMap<String,Object>> batchGet(@PathVariable("subjectTestId") Long subjectTestId) {
        //如果根据subjectTestId去subjectTest表查询出来tested是1，表示该试卷已经被考过了，那么就不应该继续考该试卷
        SubjectTest subjectTest = subjectTestMapper.selectById(subjectTestId);
        if (subjectTest.getTested()==1) {
            throw new ElearningMysqlException(30044,"获取失败！该试卷已经被考过了");
        }
        List<Long> questionIds = subjectTestRelationQuestionMapper.selectQuestionIds(subjectTestId);
        List<Question> questions = null;
        if (!questionIds.isEmpty()) {
           questions = questionMapper.selectBatchIds(questionIds);
        }
        HashMap<String,Object> data = new HashMap<>();
        data.put("result", questions);
        data.put("time", questions.size()*60);
        return Result.ok(data);
    }

    @Resource
    private SubjectTestMapper subjectTestMapper;

    @Resource
    private SubjectTestRelationQuestionMapper subjectTestRelationQuestionMapper;

    @Resource
    private QuestionMapper questionMapper;
}

